/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package javadrone.util;

//~--- non-JDK imports --------------------------------------------------------

import com.lti.civil.*;
import com.lti.civil.awt.AWTImageConverter;

//~--- JDK imports ------------------------------------------------------------

import java.awt.image.BufferedImage;

import java.io.*;

import java.util.Calendar;
import java.util.List;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

/**
 *
 * @author Some_guy_idk
 */
public class ImageStream implements CaptureObserver {
    private CaptureStream captureStream = null;
    private byte[]        bytes;
    private boolean       takeShot, shotCaptured;

    public ImageStream() {
        CaptureSystemFactory factory = DefaultCaptureSystemFactorySingleton.instance();
        CaptureSystem        system;

        try {
            system = factory.createCaptureSystem();
            system.init();

            List list = system.getCaptureDeviceInfoList();
            int  i    = 0;

            if (i < list.size()) {
                CaptureDeviceInfo info = (CaptureDeviceInfo) list.get(i);

                System.out.println(
                    (new StringBuilder()).append("Device ID ").append(i).append(": ").append(
                        info.getDeviceID()).toString());
                System.out.println(
                    (new StringBuilder()).append("Description ").append(i).append(": ").append(
                        info.getDescription()).toString());
                captureStream = system.openCaptureDeviceStream(info.getDeviceID());
                captureStream.setObserver(ImageStream.this);
            }
        } catch (CaptureException ex) {
            ex.printStackTrace();
        }
    }

    public void takeShot() {
        takeShot = true;
    }

    public void start() throws CaptureException {
        captureStream.start();
    }

    public void stop() throws CaptureException {
        captureStream.stop();
    }

    @Override
    public void onNewImage(CaptureStream stream, Image image) {
        if (!takeShot) {
            return;
        }

        byte[] bytes;

        takeShot = false;
        System.out.println("New Image Captured");

        if (image == null) {
            return;
        }

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            ImageIO.write(AWTImageConverter.toBufferedImage(image), "jpeg", os);
            bytes = os.toByteArray();
            os.close();
            this.bytes = new byte[bytes.length];
            System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
            System.out.println("Shot taken..." + this.bytes);
            shotCaptured = true;
        } catch (Exception e) {
            e.printStackTrace();
            bytes = null;
        } catch (Throwable t) {
            t.printStackTrace();
            bytes = null;
        }

        if (bytes == null) {
            return;
        }
    }

    public byte[] getBytes() {
        if (shotCaptured == true) {
            shotCaptured = false;

            return bytes;
        }

        return null;
    }

    public boolean isShotCaptured() {
        return shotCaptured;
    }

    @Override
    public void onError(CaptureStream stream, CaptureException ce) {
        System.out.println("Error!");
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
